#include <grpcpp/client_context.h>
#include <grpcpp/grpcpp.h>

#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
#include <grpcpp/support/status.h>
#include <iostream>
#include <memory>

#include "myproto/helloworld.grpc.pb.h"

class GreeterClient{
  public:
    GreeterClient(std::shared_ptr<grpc::Channel> channel)
      : stub_(helloworld::Greeter::NewStub(channel)) {}

    std::string SayHello(const std::string& user){
      helloworld::HelloRequest request;
      request.set_name(user);

      helloworld::HelloReply reply;
      grpc::ClientContext context;

      grpc::Status status = stub_->SayHello(&context, request, &reply);

      if (status.ok())
      {
        return reply.message();
      }
      else
      {
        std::cout << status.error_code() << ": " << status.error_message() << std::endl;
        return "RPC failed";
      }
    }
    private:
    std::unique_ptr<helloworld::Greeter::Stub> stub_;
};



int main(int argc, char *argv[])
{
  std::string target_str = "localhost:50051";
  //Setup request
  GreeterClient greeter(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
  std::string user("fuck world");
  std::string reply = greeter.SayHello(user);
  std::cout << "Greeter received: " << reply << std::endl;
  //auto channel = grpc::CreateChannel("localhost:5051", grpc::InsecureChannelCredentials());
  return 0;
}
